package worker

import (
	"crawler/util"
	"fmt"
	"os"
	"sync"
)

func AnJuKeWorking() {
	//创建年份数组和地区数组
	var yearArray = [...]string{"2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023"}
	var partArray = [...]string{"zhengxiangb", "yanfengb", "shigub", "zhu", "lixinkaifaqu", "huaxinkaifaqua"}
	var waitGroup sync.WaitGroup
	//设置计数器，数值即为goroutine的个数
	waitGroup.Add(len(yearArray) * len(partArray))
	for _, year := range yearArray {
		for _, part := range partArray {
			//启动一个goroutine
			go anJuKeWorking(year, part, &waitGroup)
		}
	}
	//主goroutine阻塞等待计数器变为0
	waitGroup.Wait()
}

func anJuKeWorking(year, part string, waitGroup *sync.WaitGroup) {
	url := "https://www.anjuke.com/fangjia/hengyang" + year + "/" + part
	result, httpGetError := util.HttpGet(url)
	if httpGetError != nil {
		return
	}
	file, createError := os.Create(".\\temp\\" + year + "年" + part + ".html")
	if createError != nil {
		return
	}
	_, writeError := file.WriteString(result)
	if writeError != nil {
		return
	}
	closeError := file.Close()
	if closeError != nil {
		return
	}
	fmt.Printf("%s年%s地区数据爬取完成\n\n", year, part)
	//goroutine执行结束后将计数器减1
	waitGroup.Done()
}

func parseAnJuKe(result string) {

}
